package com.atguigu.gmall.activity.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.activity.service.SeckillGoodsService;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.activity.SeckillOrder;
import com.atguigu.gmall.model.activity.UserRecode;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @Author WYL
 * @Date 2021/7/17 9:01
 * @Description: 秒杀商品信息的controller层
 * @Version 1.0.1
 */
@RestController
@RequestMapping("/seckill")
public class SeckillGoodsController {


    @Resource
    private SeckillGoodsService seckillGoodsService;

    /**
     * 返回全部列表
     * @param time 当前时间段
     * @return  Result
     */
    @GetMapping("/findAll/{time}")
    public Result findAll(@PathVariable("time") String time) {
        return Result.ok(seckillGoodsService.findAll(time));
    }

    /**
     *  根据商品id和当前时间段获取商品详情
     * @param time 当前开始时间段
     * @param skuId 秒杀商品Id
     * @return  Result
     */
    @GetMapping("/get/{time}/{skuId}")
    public Result getSeckillGoods(@PathVariable("time") String time,
                                  @PathVariable("skuId") String skuId) {
        return Result.ok(seckillGoodsService.getSeckillGoodsById(time, skuId));
    }

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RabbitService rabbitService;

    /**
     * 根据用户和商品ID实现秒杀下单
     * @param goodsId 商品ID
     * @param num 商品数量
     * @param time 当前开始时间段
     * @return Result
     */
    @GetMapping("/order/{goodsId}/{num}/{time}")
    public Result seckillOrder(@PathVariable("goodsId")String goodsId,@PathVariable("num") Integer num,@PathVariable("time")  String time){
        //获取当前登录用户的id
        String userId = "wyl";
        //将用户请求包装为排队对象
        UserRecode userRecode = new UserRecode();
        //记录要购买的商品
        userRecode.setGoodsId(goodsId);
        //商品所在的时间段
        userRecode.setTime(time);
        //记录用户购买的数量
        userRecode.setNum(num);
        //记录排队时间
        userRecode.setCreateTime(new Date());
        //记录为排队中状态
        userRecode.setStatus(1);
        //记录购买人
        userRecode.setUserId(userId);
        //发送用户排队消息
        rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_SECKILL_USER, MqConst.ROUTING_SECKILL_USER, JSONObject.toJSONString(userRecode));
        //保存用户的排队信息在redis中方便用户查询
        stringRedisTemplate.boundValueOps(RedisConst.SECKILL_USER + userId).set(JSONObject.toJSONString(userRecode));
        //返回消息
        return Result.ok("排队中!");
    }

    /**
     * 查询排队信息
     * @return Result
     */
    @GetMapping("/findUserRecode")
    public Result findUserRecode() {
        //获取当前登录用户的id
        String userId = "wyl";
        //获取排队信息
        String stringUserRecode = stringRedisTemplate.boundValueOps(RedisConst.SECKILL_USER + userId).get();
        UserRecode userRecode = JSONObject.parseObject(stringUserRecode, UserRecode.class);
        //返回
        return Result.ok(userRecode);
    }


    /**
     * 确认秒杀订单,补全收货地址信息
     * @return
     */
    @PostMapping("/seckill/confirm/order")
    public Result confirmOrder(@RequestParam("addressInfo") Map<String,String> addressInfo){
        //获取当前登录用户的id
        String userId = "wyl";
        //获取redis中的订单信息
        SeckillOrder seckillOrder = (SeckillOrder)stringRedisTemplate.boundHashOps(RedisConst.SECKILL_ORDER).get(userId);
        if(seckillOrder != null){
            //收货地址
            seckillOrder.setAddress(addressInfo.get("address"));
            //收货人
            seckillOrder.setReceiver(addressInfo.get("receiver"));
            //手机号
            seckillOrder.setMobile(addressInfo.get("mobile"));
            //更新订单信息
            stringRedisTemplate.boundHashOps(RedisConst.SECKILL_ORDER).put(userId, seckillOrder);
            //更新排队信息,提供用户查询
            UserRecode userRecode = JSONObject.parseObject(stringRedisTemplate.boundValueOps(RedisConst.SECKILL_USER + userId).get(),UserRecode.class);
            userRecode.setStatus(3);
            stringRedisTemplate.boundValueOps(RedisConst.SECKILL_USER + userId).set(JSONObject.toJSONString(userRecode));
            //返回订单确认成功,引导用户跳转至付款界面
            return Result.ok("确认成功,即将进入付款页面");
        }
        return Result.fail("订单不存在");
    }
}
